package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.db.domain.SelfMarketOrderGoodsInfo;
import com.cskaoyan.market.db.domain.SelfMarketOrderInfo;
import com.cskaoyan.market.service.MarketStatService;
import com.cskaoyan.market.service.impl.MarketStatServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("admin/stat")
public class StatusController {
    @Autowired
    private MarketStatService marketStatService;
    @GetMapping("user")
    private void user(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String,Object> data=marketStatService.user();
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok(data)));
    }
    @GetMapping("order")
    private void order(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<SelfMarketOrderInfo> orderInfos = marketStatService.getOrderInfo();
        if (orderInfos.size()>0){
            HashMap<String, Object> datas = new HashMap<>();
            List<String> strings = new ArrayList<>();
            strings.add("day");
            strings.add("orders");
            strings.add("customers");
            strings.add("amount");
            strings.add("pcr");
            datas.put("columns",strings);
            datas.put("rows",orderInfos);
            Object ok = ResponseUtil.ok(datas);
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            String s = objectMapper.writeValueAsString(ok);

            resp.getWriter().println(s);
        }else {
            Object fail = ResponseUtil.fail();
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            String s = objectMapper.writeValueAsString(fail);
            resp.getWriter().println(s);
        }
    }
    @GetMapping("goods")
    private void goods(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<SelfMarketOrderGoodsInfo> productsInfos = marketStatService.getProductsInfo();
        if (productsInfos.size()>0){
            HashMap<String, Object> datas = new HashMap<>();
            List<String> strings = new ArrayList<>();
            strings.add("day");
            strings.add("orders");
            strings.add("products");
            strings.add("amount");
            datas.put("columns",strings);
            datas.put("rows",productsInfos);
            Object ok = ResponseUtil.ok(datas);
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            String s = objectMapper.writeValueAsString(ok);

            resp.getWriter().println(s);
        }else {
            Object fail = ResponseUtil.fail();
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            String s = objectMapper.writeValueAsString(fail);
            resp.getWriter().println(s);
        }
    }
}
